const app = getApp();
var openid = wx.getStorageSync('openid');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    content: {
      isLike: false,
    },
    commentList: [],
    extendId: '',
    commentContent: '', //评论
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = options.id;
    var that = this;
    that.setData({
      extendId: id,
    });
    wx.request({
      url: app.config.serverCfg.domain.topicDetail,
      data: {
        id: id,
        openid: openid,
      },
      success(res) {
        that.setData({
          content: res.data.data,
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.commentList = this.selectComponent('#commentList');
    this.commentList.initData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 用户点赞百科内容
   */
  doLIke: function(e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    var like = 1;
    if (that.data.content.isLike === true) {
      like = -1;
    }
    wx.request({
      url: app.config.serverCfg.domain.likeAdd,
      data: {
        extendId: id,
        type: 3,
        like: like,
        openid: openid
      },
      success(res) {
        if (res.data.retcode == 2000000) {
          that.setData({
            'content.isLike': !that.data.content.isLike,
          })
        }
      }
    })
  },
  //添加评论操作，判断是否已经登录
  addComment: function(e) {
    var that = this;
    openid = wx.getStorageSync('openid');
    var common = require('../../../utils/common.js');
    if (!openid) {
      common.getToLogin();
      return false;
    }
    if (that.data.commentContent == '') {
      wx.showToast({
        title: '评论内容不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: app.config.serverCfg.domain.commentAdd,
      data: {
        extendId: that.data.extendId,
        type: 2,
        content: that.data.commentContent,
        openid: openid
      },
      success(res) {
        if (res.data.retcode == 2000000) {
          that.setData({
            commentContent: '',
          })
          wx.showToast({
            title: '发布成功',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    //先静态添加评论到列表
    var user = {
      content: that.data.commentContent,
      userInfo: {
        nick: app.globalData.userInfo.nickName,
        portrait: app.globalData.userInfo.avatarUrl
      }
    };
    this.commentList.addData(user);
  },
  changeInput(e) {
    let changed = {};
    let prop = e.currentTarget.dataset.prop
    changed[prop] = e.detail.value;
    this.setData(changed)
  },
})